﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using XStudio.XCRF.Data.Entities.LabCheck;
using XStudio.XCRF.Web.Models.Items;

namespace XStudio.XCRF.Web.Export
{
    public class LabCheckPanelCreator
    {

        public Panel createLabCheckPanel(LabCheck labCheck)
        {
            Panel panel = new Panel();

            if (labCheck == null)
                return panel;

            Panel p_title = new Panel();
            p_title.Width = Unit.Parse("100%");
            p_title.HorizontalAlign = HorizontalAlign.Center;
            Label lbl_general = new Label();
            lbl_general.Font.Bold = true;
            lbl_general.Font.Size = FontUnit.Larger;
            lbl_general.Text = "实验室检查";
            p_title.Controls.Add(lbl_general);
            panel.Controls.Add(p_title);

            Label lbl_checkDate = new Label();
            lbl_checkDate.Text = "送检日期：" + labCheck.Time.ToShortDateString();
            panel.Controls.Add(lbl_checkDate);

            Table table = new Table();
            table.Width = Unit.Parse("100%");
            table.CellPadding = 0;
            table.CellSpacing = 0;

            TableRow head_row = new TableRow();
            head_row.Cells.Add(createCell("检查项目", true, Unit.Parse("30%"), HorizontalAlign.Center, 2, 1));
            head_row.Cells.Add(createCell("检查结果", true, Unit.Parse("20%"), HorizontalAlign.Center, 1, 1));
            head_row.Cells.Add(createCell("检查项目", true, Unit.Parse("30%"), HorizontalAlign.Center, 2, 1));
            head_row.Cells.Add(createCell("检查结果", true, Unit.Parse("20%"), HorizontalAlign.Center, 1, 1));
            table.Rows.Add(head_row);

            TableRow row1 = new TableRow();
            row1.Cells.Add(createCell("血常规",true,Unit.Parse("10%"),HorizontalAlign.Center,1,5));
            row1.Cells.Add(createCell("血小板（RBC）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row1.Cells.Add(createCell(labCheck.RBC.ToString() + "×10<sup>12</sup>/L", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row1.Cells.Add(createCell("肝肾功能", true, Unit.Parse("10%"), HorizontalAlign.Center, 1, 4));
            row1.Cells.Add(createCell("谷丙转氨酶（ALT）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row1.Cells.Add(createCell(labCheck.ALT.ToString() + "U/L", true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row1);

            TableRow row2 = new TableRow();
            row2.Cells.Add(createCell("血小板（PLT）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row2.Cells.Add(createCell(labCheck.PLT.ToString() + "×10<sup>9</sup>/L", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row2.Cells.Add(createCell("谷草转氨酶（AST）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row2.Cells.Add(createCell(labCheck.AST.ToString() + "U/L", true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row2);

            TableRow row3 = new TableRow();
            row3.Cells.Add(createCell("血红蛋白（Hb）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row3.Cells.Add(createCell(labCheck.Hb.ToString() + "g/L", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row3.Cells.Add(createCell("尿素氮（BUN）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row3.Cells.Add(createCell(labCheck.BUN.ToString() + "mmol/L", true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row3);

            TableRow row4 = new TableRow();
            row4.Cells.Add(createCell("白细胞（WBC）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row4.Cells.Add(createCell(labCheck.WBC.ToString() + "×10<sup>9</sup>/L", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row4.Cells.Add(createCell("肌酐（SCr）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row4.Cells.Add(createCell(labCheck.SCr.ToString() + "µmol/L", true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row4);

            TableRow row5 = new TableRow();
            row5.Cells.Add(createCell("中性粒细胞（NEU）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row5.Cells.Add(createCell(labCheck.NEU.ToString() + "%", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row5.Cells.Add(createCell("免疫功能相关", true, Unit.Parse("10%"), HorizontalAlign.Center, 1, 6));
            row5.Cells.Add(createCell("自然杀细胞（NK）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row5.Cells.Add(createCell(labCheck.NK.ToString() + "%", true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row5);

            TableRow row6 = new TableRow();
            row6.Cells.Add(createCell("肿瘤标志物", true, Unit.Parse("10%"), HorizontalAlign.Center, 1, 3));
            row6.Cells.Add(createCell("CEA", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row6.Cells.Add(createCell(labCheck.CEA.ToString() + "ng/ml", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row6.Cells.Add(createCell("T细胞CD3", true, Unit.Parse("20%"), HorizontalAlign.Center,1,2));
            row6.Cells.Add(createCell(labCheck.CD3.ToString() + "%", true, Unit.Parse("20%"), HorizontalAlign.Center, 1, 2));
            table.Rows.Add(row6);

            TableRow row7 = new TableRow();
            row7.Cells.Add(createCell("CA50", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row7.Cells.Add(createCell(labCheck.CA50.ToString() + "ng/ml", true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row7);

            TableRow row8 = new TableRow();
            row8.Cells.Add(createCell("CA199", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row8.Cells.Add(createCell(labCheck.CA199.ToString() + "ng/ml", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row8.Cells.Add(createCell("T细胞CD4", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row8.Cells.Add(createCell(labCheck.CD4.ToString() + "%", true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row8);

            TableRow row9 = new TableRow();
            row9.Cells.Add(createCell("粪常规", true, Unit.Parse("10%"), HorizontalAlign.Center, 1, 2));
            row9.Cells.Add(createCell("白细胞", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row9.Cells.Add(createCell(MarkInfo.GetName((byte)labCheck.BXB), true, Unit.Parse("20%"), HorizontalAlign.Center));
            row9.Cells.Add(createCell("T细胞CD8", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row9.Cells.Add(createCell(labCheck.CD8.ToString() + "%", true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row9);

            TableRow row10 = new TableRow();
            row10.Cells.Add(createCell("粪便潜血试验", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row10.Cells.Add(createCell(MarkInfo.GetName((byte)labCheck.FB), true, Unit.Parse("20%"), HorizontalAlign.Center));
            row10.Cells.Add(createCell("CD4/CD8", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row10.Cells.Add(createCell(labCheck.CD4_CD8.ToString(), true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row10);

            TableRow row11 = new TableRow();
            row11.Cells.Add(createCell("尿常规", true, Unit.Parse("10%"), HorizontalAlign.Center, 1, 2));
            row11.Cells.Add(createCell("尿蛋白（PRO）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row11.Cells.Add(createCell(MarkInfo.GetName((byte)labCheck.PRO), true, Unit.Parse("20%"), HorizontalAlign.Center));
            row11.Cells.Add(createCell("", true, Unit.Parse("10%"), HorizontalAlign.Center));
            row11.Cells.Add(createCell("尿白细胞（LEU）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row11.Cells.Add(createCell(MarkInfo.GetName((byte)labCheck.LEU), true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row11);

            TableRow row12 = new TableRow();
            row12.Cells.Add(createCell("尿糖（GLU）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row12.Cells.Add(createCell(MarkInfo.GetName((byte)labCheck.GLU), true, Unit.Parse("20%"), HorizontalAlign.Center));
            row12.Cells.Add(createCell("", true, Unit.Parse("10%"), HorizontalAlign.Center));
            row12.Cells.Add(createCell("尿红细胞（BLD）", true, Unit.Parse("20%"), HorizontalAlign.Center));
            row12.Cells.Add(createCell(MarkInfo.GetName((byte)labCheck.BLD), true, Unit.Parse("20%"), HorizontalAlign.Center));
            table.Rows.Add(row12);

            TableRow row13 = new TableRow();
            row13.Cells.Add(createCell("特殊检查", true, Unit.Parse("100%"), HorizontalAlign.Center,6,1));
            table.Rows.Add(row13);

            TableRow row14 = new TableRow();
            row14.Cells.Add(createCell("心电图（日期：" + labCheck.ECGTime.ToString() + "）：" + labCheck.ECG, true, Unit.Parse("100%"), HorizontalAlign.Left, 6, 1));
            table.Rows.Add(row14);

            TableRow row15 = new TableRow();
            row15.Cells.Add(createCell("肠镜（日期：" + labCheck.EnteroscopeTime.ToString() + "）：" + labCheck.Enteroscope, true, Unit.Parse("100%"), HorizontalAlign.Left, 6, 1));
            table.Rows.Add(row15);

            TableRow row16 = new TableRow();
            row16.Cells.Add(createCell("胸部CT（日期：" + labCheck.ChestCTTime.ToString() + "）：" + labCheck.ChestCT, true, Unit.Parse("100%"), HorizontalAlign.Left, 6, 1));
            table.Rows.Add(row16);

            TableRow row17 = new TableRow();
            row17.Cells.Add(createCell("B超（日期：" + labCheck.BUltraTime.ToString() + "）：" + labCheck.BUltra, true, Unit.Parse("100%"), HorizontalAlign.Left, 6, 1));
            table.Rows.Add(row17);

            TableRow row18 = new TableRow();
            row18.Cells.Add(createCell("腹部CT（日期：" + labCheck.AbdomenCTTime.ToString() + "）：" + labCheck.AbdomenCT, true, Unit.Parse("100%"), HorizontalAlign.Left, 6, 1));
            table.Rows.Add(row18);

            TableRow row19 = new TableRow();
            row19.Cells.Add(createCell("盆腔CT（日期：" + labCheck.PelvicCTTime.ToString() + "）：" + labCheck.PelvicCT, true, Unit.Parse("100%"), HorizontalAlign.Left, 6, 1));
            table.Rows.Add(row19);

            TableRow row20 = new TableRow();
            row20.Cells.Add(createCell("盆腔MRI（日期：" + labCheck.PelvicMRITime.ToString() + "）：" + labCheck.PelvicMRI, true, Unit.Parse("100%"), HorizontalAlign.Left, 6, 1));
            table.Rows.Add(row20);

            TableRow row21 = new TableRow();
            row21.Cells.Add(createCell("PetCT（日期：" + labCheck.PetCTTime.ToString() + "）：" + labCheck.PetCT, true, Unit.Parse("100%"), HorizontalAlign.Left, 6, 1));
            table.Rows.Add(row21);

            panel.Controls.Add(table);

            return panel;
        }

        private TableCell createCell(String txt, Boolean haveborder, Unit width, HorizontalAlign align)
        {
            TableCell cell = new TableCell();
            cell.Text = txt;
            if (haveborder)
            {
                cell.BorderColor = System.Drawing.Color.Black;
                cell.BorderStyle = BorderStyle.Solid;
                cell.BorderWidth = Unit.Parse("1px");
            }
            if (width != null)
            {
                cell.Width = width;
            }
            cell.HorizontalAlign = align;
            return cell;
        }

        private TableCell createCell(String txt, Boolean haveborder, Unit width, HorizontalAlign align , int colspan , int rowspan)
        {
            TableCell cell = new TableCell();
            cell.Text = txt;
            if (haveborder)
            {
                cell.BorderColor = System.Drawing.Color.Black;
                cell.BorderStyle = BorderStyle.Solid;
                cell.BorderWidth = Unit.Parse("1px");
            }
            if (width != null)
            {
                cell.Width = width;
            }
            cell.HorizontalAlign = align;
            if (colspan > 1)
                cell.ColumnSpan = colspan;
            if (rowspan > 1)
                cell.RowSpan = rowspan;
            return cell;
        }

    }
}